#include <QtGui/QtGui>
#include "mainwindow.h"

	log4cxx::LoggerPtr
		MainWindow::m_log(log4cxx::Logger::getLogger("MainWindow"));


///The Construtor for the MainWindow class
/*!
This function creates all necessary widgets for the main window.

This class inherits from the Qt MainWindow class to create a standard 

Qt main window.
*/
MainWindow::MainWindow()
{
	createActions();
    createMenus();
	createToolBars();
    createStatusBar();
	createGLWidget();
	std::cout << " MainWindow constuctor successful.." << std::endl;;
}


///The close event function........
/*!

*/
void MainWindow::closeEvent(QCloseEvent *event)
{
	LOG4CXX_DEBUG(m_log, "closeEvent: ")
	event->accept();
}

void MainWindow::resizeGL(int w, int h)
{
	this->resize(w,h);
	openglwidget->resize(int(w*3/4),int(h*3/4));
	setCentralWidget(openglwidget);
}



///This function creates an OpenGL widget
/*!
The OpenGL windget is used to display all openGL data including the 

mesh files as 3D objects.
*/
void MainWindow::createGLWidget()
{
	openglwidget = new GLWidget(this);
	setCentralWidget(openglwidget);
}

///This function creates all Qt actions used in the main window
/*!
The actions are saved as instance variable within the class.
*/
void MainWindow::createActions()
{
	openAction = new QAction(tr("&Open..."), this);
    openAction->setIcon(QIcon(":/images/open.png"));
    openAction->setShortcut(QKeySequence::Open);
    openAction->setStatusTip(tr("Open file"));
	connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

	saveAction = new QAction(tr("&Save"), this);
    saveAction->setIcon(QIcon(":/images/save.png"));
    saveAction->setShortcut(QKeySequence::Save);
    saveAction->setStatusTip(tr("Save to disk"));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));

    saveAsAction = new QAction(tr("Save &As..."), this);
    saveAsAction->setStatusTip(tr("Save under a new "
                                  "name"));
    connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));

	exitAction = new QAction(tr("E&xit"), this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("Exit the application"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

	aboutAction = new QAction(tr("About"), this);
    aboutAction->setStatusTip(tr("Show the About box"));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
}

///This function creates the menus for the main window
/*!
The menus are saved as instance variable in the class
*/
void MainWindow::createMenus()
{
	fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);
    fileMenu->addAction(saveAsAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

	helpMenu = menuBar()->addMenu(tr("&Help"));
	helpMenu->addAction(aboutAction);
}


///This function creates the toolbar for the main window
/*!
The toolbar is stored as an instance variable in the class
*/
void MainWindow::createToolBars()
{
	fileToolBar = addToolBar(tr("&File"));
    fileToolBar->addAction(openAction);
    fileToolBar->addAction(saveAction);
	fileToolBar->addSeparator();
}

///This function shows a statusbar message
/*!
This function uses the default statusbar created by the main window.
*/
void MainWindow::createStatusBar()
{
	statusBar()->showMessage(tr("CPMainWindow"));
}


///This function provides the top level Open File functionality
/*!
The funtion uses the OpenGL widget to open and display a mesh file 

in 3D within the OpenGL widget.
*/
void MainWindow::open()
{
	
	LOG4CXX_DEBUG(m_log, "open: this does nothing")

	fileDialog1 = new QFileDialog;

	QString fileName = /*QFileDialog::*/fileDialog1->getOpenFileName(this,
                                   tr("Open Mesh"), ".",
                                   tr("Mesh File (*.ply);;""Mesh File (*.off)"));


    if (fileName.isEmpty())
	{
		LOG4CXX_ERROR(m_log, "open: Read file unsuccessful")
		QString msg = "Cannot read mesh from file:\n '";
        msg += fileName;
        msg += "'";
        QMessageBox::critical( this, windowTitle(), msg);
	}
	else
	{
		LOG4CXX_DEBUG(m_log, "open: Read file successful")
		QString msg = "Read mesh from file:\n '";
		msg += fileName;
		msg += "' successfully\n";
		msg += "Info:\nVertices= ";		
		QMessageBox::information( this, windowTitle(), msg);		
	}
}


///This function provides the top level Save File functionality 
/*!
Calls the appropriate OpenGL widget function to save the current mesh object as a mesh file.
*/
bool MainWindow::save()
{
	LOG4CXX_DEBUG(m_log, "save: does nothing")
	createGLWidget();
	return true;
}

///This function provides the top level Save As File functionality 
/*!
Calls the appropriate OpenGL widget function to save the current mesh object as a new mesh file.
*/
bool MainWindow::saveAs()
{
	LOG4CXX_DEBUG(m_log, "saveAs: does nothing")	
	return true;
}

///This function shows an About window for the application 
/*!
Creates and displays a Qt about message box.
*/
void MainWindow::about()
{
	QMessageBox::about(this, tr("About Application"),
            tr("<h2>VerificationGUI v0.1</h2>"
               "<p>Copyright &copy; 2011 UTS."
               "<p>This application is a blank slate"
               "... "));


}

void MainWindow::updateStatusBar()
{

}

